package project.ccso.top.service.impl;

import java.sql.ResultSet;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import project.ccso.top.bean.GridPageBean;
import project.ccso.top.dao.CityDao;
import project.ccso.top.dao.ProvinceDao;
import project.ccso.top.dao.impl.CityDaoImpl;
import project.ccso.top.dao.impl.ProvinceDaoImpl;
import project.ccso.top.entity.City;
import project.ccso.top.entity.Province;
import project.ccso.top.enumerate.OpenStatus;
import project.ccso.top.service.CityService;
import project.ccso.top.service.ProvinceService;
import project.ccso.top.util.DBUtil;
import project.ccso.top.util.DateUtil;

/**
 * 省份Service的实现类
 * @author aibinxiao
 * @date 2018年8月23日 上午11:58:05
 */
public class ProvinceServiceImpl implements ProvinceService {
	// 日志
	private static Logger logger = LoggerFactory.getLogger(ProvinceServiceImpl.class);
	// 注入provinceDao
	private ProvinceDao provinceDao = new ProvinceDaoImpl();
	// 注入CityDao
	private CityDao cityDao = new CityDaoImpl();
	// 注入CityService
	private CityService cityService = new CityServiceImpl();

	@Override
	public GridPageBean<Province> findByPage(String pageNoStr, String pageSizeStr, String id, String name, String startDate, String endDate) throws Exception {
		Integer pageNum = 1;
		Integer pageSize = 10;
		if(!StringUtils.isEmpty(pageNoStr) && !StringUtils.isEmpty(pageSizeStr)){
			boolean flag = true;
			try {
				pageNum = Integer.valueOf(pageNoStr);
				pageSize = Integer.valueOf(pageSizeStr);
			} catch (Exception e) {
				flag = false;
			}
			if(!flag) {
				throw new Exception("参数page或rows不是int类型或者超出int范围！");
			}
		}
		int records = provinceDao.countByParams(id, name, startDate, endDate);
		GridPageBean<Province> result = new GridPageBean<Province>(pageNum, records, pageSize);
		//查询分页后的具体记录
		if(records <= result.getPageSize()){
			List<Province> rows = provinceDao.findPageByParams(id,name,startDate,endDate,result.getStartIndex(),records);
			result.setRows(rows);
		} else{
			List<Province> rows = provinceDao.findPageByParams(id,name,startDate,endDate,result.getStartIndex(),result.getPageSize());
			result.setRows(rows);
		}
		result.setPage(pageNum);
		result.setRecords(records);
		result.setTotal(result.getTotal());
		return result;
	}

	/**
	 * 根据id查找省份
	 */
	@Override
	public Province findById(String idStr) throws Exception {
		int id = 1;
		try {
			id = Integer.parseInt(idStr);
		} catch (Exception e) {
			throw new Exception("参数id不是整数类型或超出int范围！");
		}
		Province province = provinceDao.findById(id);
		return province;
	}
	//根据id修改省份
	@Override
	public boolean saveOrUpdateById(String id, String provinceName) throws Exception {
		if(StringUtils.isEmpty(provinceName)) {
			throw new Exception("参数provinceName为NULL或空字符串！");
		}
		return provinceDao.saveOrUpdateById(id, provinceName);
	}
	//查询所有的省份的id和name
	@Override
	public List<Province> selectAllProvince(){
		return provinceDao.selectAllProvince();
	}

	@Override
	public boolean updateShieldStatus(String idStr, String statusStr) throws Exception {
		boolean flag = false;
		int id = 1;
		int transStatus;
		if(StringUtils.isEmpty(idStr)) {
			throw new Exception("参数id为NULL或空字符串！");
		}else {
			try {
				id = Integer.parseInt(idStr);
			} catch (Exception e) {
				e.printStackTrace();
				logger.info(e.getMessage());
				throw new Exception("参数id不是int类型或超出int范围！");
				
			}
		}
		if(StringUtils.isEmpty(statusStr)) {
			throw new Exception("参数transStatus为NULL或空字符串！");
		}else {
			try {
				transStatus = Integer.parseInt(statusStr);
			} catch (Exception e) {
				e.printStackTrace();
				logger.info(e.getMessage());
				throw new Exception("参数transStatus不是int类型或超出int范围！");
			}
		}
		flag = provinceDao.updateShieldStatus(id, transStatus);
		return flag;
	}


	/**
	 * 3.根据省份id获取省份信息Service实现类
	 *//*
	@Override
	public Province findById(String idStr) {
		Province province = null;
		if(!StringUtils.isEmpty(idStr)){
			Integer id = Integer.valueOf(idStr);
			province = provinceDao.findProvinceById(id);
		}
		return province;
	}

	*//**
	 * 4.根据id保存或者更新省份信息Service实现类
	 *//*
	@Override
	public void save(String idStr, String name) throws Exception {
		Province province = new Province();
		// 如果id不为空则是修改更新
		if(!StringUtils.isEmpty(idStr)){
			Integer id = Integer.valueOf(idStr);
			province = provinceDao.findProvinceById(id);
		}else{
			// id为空则是添加新的三级分类
			province.setCreateTime(DateUtil.DateToStr(new Date()));
			province.setStatus(OpenStatus.OPEN.getStatusCode());
		}
		if(!StringUtils.isEmpty(name)){
			province.setName(name);
		}
		province.setAlterTime(DateUtil.DateToStr(new Date()));
		provinceDao.saveProvince(province);
	}

	*//**
	 * 5.更新省份信息的启用状态Service实现类
	 *//*
	@Override
	public void updateStatus(String idStr, String statusStr) throws Exception {
		Integer status = null;
		Integer id = null;
		if(StringUtils.isEmpty(idStr)){
			throw new Exception("id参数不能为空");
		}else{
			id = Integer.valueOf(idStr);
		}
		if(StringUtils.isEmpty(statusStr)){
			throw new Exception("转换状态参数不能为空");
		}
		if(!StringUtils.isEmpty(statusStr)){
			status = Integer.valueOf(statusStr);
		}
		//开启
		if(OpenStatus.OPEN.getStatusCode()==status){
			provinceDao.updateStauts(id, OpenStatus.OPEN.getStatusCode());
			updateAllCityStatusByOneId(status,id);
		}
		//屏蔽
		if(OpenStatus.SHIELD.getStatusCode()==status){
			provinceDao.updateStauts(id, OpenStatus.SHIELD.getStatusCode());
			updateAllCityStatusByOneId(status,id);
		}
	}
	
	*//**
	 * 6.根据一级分类ID更新所有的二级分类状态
	 * @param status
	 * @param id
	 * @throws Exception
	 *//*
	private void updateAllCityStatusByOneId(Integer status, Integer id) throws Exception {
		List<City> listCity = cityDao.findAllCityByProvinceId(id);
		if(listCity!=null && listCity.size()>0){
			logger.info("该省份下查询到["+listCity.size()+"]个城市,正在同步"+getTransStatus(status));
			for (City city : listCity) {
				cityService.updateStatus(city.getId().toString(), status.toString());
			}
		}else{
			logger.info("该省份下未查询到城市");
		}
	}
	
	*//**
	 * 7.获取正在如何更新状态
	 * @param status
	 * @return
	 *//*
	public String getTransStatus(Integer status){
		String result = null;
		if(status!=null){
			if(status==0){
				result = "启用";
			}else{
				result = "屏蔽";
			}
		}
		return result;
	}*/

}
